home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Video / motion.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-06  |  7.0 KB  |  241 lines

  1. /* motion.c, motion vector decoding                                         */
  2.  
  3. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  4.  
  5. /*
  6.  * Disclaimer of Warranty
  7.  *
  8.  * These software programs are available to the user without any license fee or
  9.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  10.  * any and all warranties, whether express, implied, or statuary, including any
  11.  * implied warranties or merchantability or of fitness for a particular
  12.  * purpose.  In no event shall the copyright-holder be liable for any
  13.  * incidental, punitive, or consequential damages of any kind whatsoever
  14.  * arising from the use of these programs.
  15.  *
  16.  * This disclaimer of warranty extends to the user of these programs and user's
  17.  * customers, employees, agents, transferees, successors, and assigns.
  18.  *
  19.  * The MPEG Software Simulation Group does not represent or warrant that the
  20.  * programs furnished hereunder are free of infringement of any third-party
  21.  * patents.
  22.  *
  23.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  24.  * are subject to royalty fees to patent holders.  Many of these patents are
  25.  * general enough such that they are unavoidable regardless of implementation
  26.  * design.
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31.  
  32. #include "config.h"
  33. #include "global.h"
  34.  
  35. char szTemp[256];
  36. /* private prototypes */
  37. static void decode_motion_vector _ANSI_ARGS_((int *pred, int r_size, int motion_code,
  38.   int motion_residualesidual, int full_pel_vector));
  39.  
  40. /* ISO/IEC 13818-2 sections 6.2.5.2, 6.3.17.2, and 7.6.3: Motion vectors */
  41. void motion_vectors(PMV,dmvector,
  42.   motion_vertical_field_select,s,motion_vector_count,mv_format,h_r_size,v_r_size,dmv,mvscale)
  43. int PMV[2][2][2];
  44. int dmvector[2];
  45. int motion_vertical_field_select[2][2];
  46. int s, motion_vector_count, mv_format, h_r_size, v_r_size, dmv, mvscale;
  47. {
  48.   if (motion_vector_count==1)
  49.   {
  50.     if (mv_format==MV_FIELD && !dmv)
  51.     {
  52.       motion_vertical_field_select[1][s] = motion_vertical_field_select[0][s] = Get_Bits(1);
  53. #ifdef TRACE
  54.       if (Trace_Flag)
  55.       {
  56.         sprintf(szTemp, "motion_vertical_field_select[][%d] (%d): %d\n",s,
  57.           motion_vertical_field_select[0][s],motion_vertical_field_select[0][s]);
  58.     //    ShowInConsole( szTemp );
  59.       }
  60. #endif /* TRACE */
  61.     }
  62.  
  63.     motion_vector(PMV[0][s],dmvector,h_r_size,v_r_size,dmv,mvscale,0);
  64.  
  65.     /* update other motion vector predictors */
  66.     PMV[1][s][0] = PMV[0][s][0];
  67.     PMV[1][s][1] = PMV[0][s][1];
  68.   }
  69.   else
  70.   {
  71.     motion_vertical_field_select[0][s] = Get_Bits(1);
  72. #ifdef TRACE
  73.     if (Trace_Flag)
  74.     {
  75.       sprintf(szTemp, "motion_vertical_field_select[0][%d] (%d): %d\n",s,
  76.         motion_vertical_field_select[0][s],motion_vertical_field_select[0][s]);
  77.               //ShowInConsole( szTemp );
  78.     }
  79. #endif /* TRACE */
  80.     motion_vector(PMV[0][s],dmvector,h_r_size,v_r_size,dmv,mvscale,0);
  81.  
  82.     motion_vertical_field_select[1][s] = Get_Bits(1);
  83. #ifdef TRACE
  84.     if (Trace_Flag)
  85.     {
  86.       sprintf(szTemp, "motion_vertical_field_select[1][%d] (%d): %d\n",s,
  87.         motion_vertical_field_select[1][s],motion_vertical_field_select[1][s]);
  88.               //ShowInConsole( szTemp );
  89.     }
  90. #endif /* TRACE */
  91.     motion_vector(PMV[1][s],dmvector,h_r_size,v_r_size,dmv,mvscale,0);
  92.   }
  93. }
  94.  
  95. /* get and decode motion vector and differential motion vector 
  96.    for one prediction */
  97. void motion_vector(PMV,dmvector,
  98.   h_r_size,v_r_size,dmv,mvscale,full_pel_vector)
  99. int *PMV;
  100. int *dmvector;
  101. int h_r_size;
  102. int v_r_size;
  103. int dmv; /* MPEG-2 only: get differential motion vectors */
  104. int mvscale; /* MPEG-2 only: field vector in frame pic */
  105. int full_pel_vector; /* MPEG-1 only */
  106. {
  107.   int motion_code, motion_residual;
  108.  
  109.   /* horizontal component */
  110.   /* ISO/IEC 13818-2 Table B-10 */
  111.   motion_code = Get_motion_code();
  112.  
  113.   motion_residual = (h_r_size!=0 && motion_code!=0) ? Get_Bits(h_r_size) : 0;
  114.  
  115. #ifdef TRACE
  116.   if (Trace_Flag)
  117.   {
  118.     if (h_r_size!=0 && motion_code!=0)
  119.     {
  120.       printf("motion_residual (");
  121.       Print_Bits(motion_residual,h_r_size,h_r_size);
  122.       printf("): %d\n",motion_residual);
  123.     }
  124.   }
  125. #endif /* TRACE */
  126.  
  127.  
  128.   decode_motion_vector(&PMV[0],h_r_size,motion_code,motion_residual,full_pel_vector);
  129.  
  130.   if (dmv)
  131.     dmvector[0] = Get_dmvector();
  132.  
  133.  
  134.   /* vertical component */
  135.   motion_code     = Get_motion_code();
  136.   motion_residual = (v_r_size!=0 && motion_code!=0) ? Get_Bits(v_r_size) : 0;
  137.  
  138. #ifdef TRACE
  139.   if (Trace_Flag)
  140.   {
  141.     if (v_r_size!=0 && motion_code!=0)
  142.     {
  143.       printf("motion_residual (");
  144.       Print_Bits(motion_residual,v_r_size,v_r_size);
  145.       printf("): %d\n",motion_residual);
  146.     }
  147.   }
  148. #endif /* TRACE */
  149.  
  150.   if (mvscale)
  151.     PMV[1] >>= 1; /* DIV 2 */
  152.  
  153.   decode_motion_vector(&PMV[1],v_r_size,motion_code,motion_residual,full_pel_vector);
  154.  
  155.   if (mvscale)
  156.     PMV[1] <<= 1;
  157.  
  158.   if (dmv)
  159.     dmvector[1] = Get_dmvector();
  160.  
  161. #ifdef TRACE
  162.   if (Trace_Flag)
  163.     printf("PMV = %d,%d\n",PMV[0],PMV[1]);
  164. #endif /* TRACE */
  165. }
  166.  
  167. /* calculate motion vector component */
  168. /* ISO/IEC 13818-2 section 7.6.3.1: Decoding the motion vectors */
  169. /* Note: the arithmetic here is more elegant than that which is shown 
  170.    in 7.6.3.1.  The end results (PMV[][][]) should, however, be the same.  */
  171.  
  172. static void decode_motion_vector(pred,r_size,motion_code,motion_residual,full_pel_vector)
  173. int *pred;
  174. int r_size, motion_code, motion_residual;
  175. int full_pel_vector; /* MPEG-1 (ISO/IEC 11172-1) support */
  176. {
  177.   int lim, vec;
  178.  
  179.   lim = 16<<r_size;
  180.   vec = full_pel_vector ? (*pred >> 1) : (*pred);
  181.  
  182.   if (motion_code>0)
  183.   {
  184.     vec+= ((motion_code-1)<<r_size) + motion_residual + 1;
  185.     if (vec>=lim)
  186.       vec-= lim + lim;
  187.   }
  188.   else if (motion_code<0)
  189.   {
  190.     vec-= ((-motion_code-1)<<r_size) + motion_residual + 1;
  191.     if (vec<-lim)
  192.       vec+= lim + lim;
  193.   }
  194.   *pred = full_pel_vector ? (vec<<1) : vec;
  195. }
  196.  
  197.  
  198. /* ISO/IEC 13818-2 section 7.6.3.6: Dual prime additional arithmetic */
  199. void Dual_Prime_Arithmetic(DMV,dmvector,mvx,mvy)
  200. int DMV[][2];
  201. int *dmvector; /* differential motion vector */
  202. int mvx, mvy;  /* decoded mv components (always in field format) */
  203. {
  204.   if (picture_structure==FRAME_PICTURE)
  205.   {
  206.     if (top_field_first)
  207.     {
  208.       /* vector for prediction of top field from bottom field */
  209.       DMV[0][0] = ((mvx  +(mvx>0))>>1) + dmvector[0];
  210.       DMV[0][1] = ((mvy  +(mvy>0))>>1) + dmvector[1] - 1;
  211.  
  212.       /* vector for prediction of bottom field from top field */
  213.       DMV[1][0] = ((3*mvx+(mvx>0))>>1) + dmvector[0];
  214.       DMV[1][1] = ((3*mvy+(mvy>0))>>1) + dmvector[1] + 1;
  215.     }
  216.     else
  217.     {
  218.       /* vector for prediction of top field from bottom field */
  219.       DMV[0][0] = ((3*mvx+(mvx>0))>>1) + dmvector[0];
  220.       DMV[0][1] = ((3*mvy+(mvy>0))>>1) + dmvector[1] - 1;
  221.  
  222.       /* vector for prediction of bottom field from top field */
  223.       DMV[1][0] = ((mvx  +(mvx>0))>>1) + dmvector[0];
  224.       DMV[1][1] = ((mvy  +(mvy>0))>>1) + dmvector[1] + 1;
  225.     }
  226.   }
  227.   else
  228.   {
  229.     /* vector for prediction from field of opposite 'parity' */
  230.     DMV[0][0] = ((mvx+(mvx>0))>>1) + dmvector[0];
  231.     DMV[0][1] = ((mvy+(mvy>0))>>1) + dmvector[1];
  232.  
  233.     /* correct for vertical field shift */
  234.     if (picture_structure==TOP_FIELD)
  235.       DMV[0][1]--;
  236.     else
  237.       DMV[0][1]++;
  238.   }
  239. }
  240.  
  241.